package com.woyowodaows.activity.mine;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.woyowodaows.R;
import com.woyowodaows.base.BaseActivity;
import com.woyowodaows.base.BaseBean;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.RegexpUtils;
import com.woyowodaows.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author haiwei
 *         修改密码
 */

public class EditPassworddActivity extends BaseActivity {
    Context context;
    @BindView(R.id.et_password_old)
    EditText etPasswordOld;
    @BindView(R.id.ll_password_old)
    LinearLayout llPasswordOld;
    @BindView(R.id.et_password_first)
    EditText etPasswordFirst;
    @BindView(R.id.et_password_second)
    EditText etPasswordSecond;
    @BindView(R.id.bt_password_commit)
    Button btPasswordCommit;

    @Override
    public int getLayoutId() {
        return R.layout.activity_edit_password;
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
        setTitleBar(false, "修改密码", "", 0, 0, null);
        context = EditPassworddActivity.this;
    }

    @OnClick(R.id.bt_password_commit)
    public void onClick() {

        final String password = etPasswordFirst.getText().toString().trim();

        final String twoPassword = etPasswordSecond.getText().toString().trim();

        final String oldPasswords = etPasswordOld.getText().toString().trim();

        if (TextUtils.isEmpty(oldPasswords)) {
            ToastUtil.show_short(context, "旧密码不能为空!");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.show_short(context, "新密码不能为空!");
            return;
        }

        if (password.length() < 6 || oldPasswords.length() < 6 || twoPassword.length() < 6 ) {
            ToastUtil.show(context, "密码长度不能少于6位");
            return;
        }

        if(!RegexpUtils.includeEnglishNumber(password)){
            ToastUtil.show(context, "输入的密码包含数字和英文");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.show_short(context, "确认密码不能为空!");
            return;
        }

        if (!password.equals(twoPassword)) {
            ToastUtil.show_short(context, "两次密码输入不一致!");
            return;
        }

        Map<String, Object> mapParameter = new HashMap<>();
//        password:weixuehu1
//        oldPassword:weixuehu
        mapParameter.put("password",password);
        mapParameter.put("oldPassword", oldPasswords);
        HttpAsyncTask.getInstance().onPostParam(this, "正在修改密码...", true, InterfaceUrl.UPDATEPASSWORD, BaseBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    BaseBean bean = (BaseBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        ToastUtil.show_short(EditPassworddActivity.this, "密码修改成功");
                        finish();
                    } else {
                        ToastUtil.show_short(EditPassworddActivity.this, bean.getMessage());
                    }
                }

            }

            @Override
            public void onError(String message) {
                ToastUtil.show_short(EditPassworddActivity.this, message);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        HttpAsyncTask.getInstance().progressBarDialog = null;
    }
}
